package com.deneme.ozveri.adapters;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.deneme.ozveri.application.OzveriMenuApplicationObject;
import com.deneme.ozveri.objects.MenuOrderItem;
import com.deneme.ozveri.objects.MenuOrderItemExtras;
import com.deneme.ozverimenu.R;

public class OrderListAdapter extends BaseAdapter {
	private Context ctxt;

	public OrderListAdapter(Context context) {

		ctxt = context;
	}

	@Override
	public int getCount() {
		return OzveriMenuApplicationObject.orders.size();
	}

	@Override
	public Object getItem(int arg0) {
		return OzveriMenuApplicationObject.orders.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout itemLayout;
		LayoutInflater inflater = (LayoutInflater) ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (OzveriMenuApplicationObject.orders == null || OzveriMenuApplicationObject.orders.size() == 0) {
			return null;
		}

		MenuOrderItem currItem = OzveriMenuApplicationObject.orders.get(position);

		itemLayout = (LinearLayout) inflater.inflate(
				R.layout.orders_item_row, parent, false);

		TextView name = (TextView) itemLayout
				.findViewById(R.id.orderitem_name_txt);

		String detailedName = currItem.getOrderName();

		for (int i = 0; i < currItem.getExtraList().keySet().size(); i++) {
			int currKey = (Integer)(currItem.getExtraList().keySet().toArray()[i]);
			int currValue = currItem.getExtraList().get(currKey);

			MenuOrderItemExtras currExtra = null;

			for (MenuOrderItemExtras extra : currItem.getExtras()) {
				if (extra.getId() == currKey) {
					currExtra = extra;
				}
			}

			if (currExtra!=null) {

				if (currValue > 1) {
					if (i==0) {
						detailedName += " ("+currExtra.getName() +"("+currValue+")";
						if (i==currItem.getExtraList().values().size()-1 )	{
							detailedName += ")";
						}
					}
					else if (i==currItem.getExtraList().values().size()-1 )			{
						detailedName += " ,"+currExtra.getName() +"("+currValue+")"+")";
					}
					else{
						detailedName += " ,"+currExtra.getName() +"("+currValue+")";
					}
				}
				else{
					if (i==0) {
						detailedName += " ("+currExtra.getName();
						if (i==currItem.getExtraList().values().size()-1 )	{
							detailedName += ")";
						}
					}
					else if (i==currItem.getExtraList().values().size()-1 )			{
						detailedName += " ,"+currExtra.getName()+")";
					}
					else{
						detailedName += " ,"+currExtra.getName();
					}
				}





			}
			else{
				Log.w(this.getClass().getName(),"Houston we got serious shit here! Extra didn't match the id");
			}






		}

		name.setText(detailedName);
		name.setTextColor(R.color.black);

		TextView price = (TextView) itemLayout.findViewById(R.id.orderitem_price_txt);
		price.setText(""+(currItem.getOrderTotalPrice()*currItem.getOrderQuantity()*1.0/100.0));
		price.setTextColor(R.color.black);


		Spinner quantity = (Spinner) itemLayout.findViewById(R.id.orderitem_quantity_spinner);
		String[] np = new String[26];
		for(int i=0;i<25;i++){
			np[i]=Integer.toString(i+1);
		}

		ArrayAdapter <String> adapter = new ArrayAdapter<String>(ctxt, R.layout.spinner_item_text, np);
		quantity.setAdapter(adapter);
		quantity.setSelection(currItem.getOrderQuantity()-1);// 1 is offset
		quantity.setOnItemSelectedListener(new SpinnerSelect(price, currItem));

		//		ArrayAdapter <String> _aa = new ArrayAdapter<String>(ctxt, android.R.layout.simple_spinner_dropdown_item,np);//array holding min and max pages
		//		quantity.setAdapter(_aa);



		//		quantity.setText(""+currItem.getOrderQuantity());
		//		quantity.setOnEditorActionListener(new DoneOnEditorActionListener(price, currItem));
		//		quantity.setInputType(InputType.TYPE_CLASS_NUMBER);
		//		quantity.setTextColor(R.color.black);
		//		quantity.setEms(10);


		ImageView cancel = (ImageView) itemLayout.findViewById(R.id.orderitem_cancel_img);
		cancel.setOnClickListener(new CancelListener(currItem));


		itemLayout.setBackgroundColor((position % 2) == 1 ? ctxt
				.getApplicationContext().getResources()
				.getColor(R.color.rowcolor0) : ctxt.getApplicationContext()
				.getResources().getColor(R.color.rowcolor1));

		return itemLayout;
	}


	private class CancelListener implements OnClickListener{


		MenuOrderItem item;


		public CancelListener(MenuOrderItem item) {
			this.item = item;
		}

		@Override
		public void onClick(View v) {

			OzveriMenuApplicationObject.orders.remove(item);
			notifyDataSetChanged();

		}

	}

	class SpinnerSelect implements OnItemSelectedListener {
		TextView price;
		MenuOrderItem item;
		public SpinnerSelect(TextView price, MenuOrderItem item) {
			this.price = price;
			this.item = item;
		}

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			if(item.getOrderQuantity() != (arg2+1))
			{
				item.setOrderQuantity(arg2+1);
				notifyDataSetChanged();
			}
				
			

			

		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}
	}





}
